const jwtUtil = require("../utils/jwtUtils");
module.exports = class user_dao extends require("../model/student_mod") {
  // 获取通知和数量
  static async getNotice(req, res) {
    let verify = await jwtUtil.verifysync(req.query.token, global.globalKey);
    if (verify.err == "error") {
      return res.json(verify.msg);
    }
    let user_classes = verify.classes;

    let { pageSize, currPage } = req.query;
    let data, total;
    if (verify.type == 3) {
      user_classes = user_classes.split(";");
      // console.log(user_classes);
      data = await this.getTeacherNoticeMod(user_classes, pageSize, currPage);
      total = await this.getTeacherNoticeTotal(user_classes);
    } else {
      data = await this.getNoticeMod(user_classes, pageSize, currPage);
      total = await this.getNoticeTotal(user_classes);
    }

    res.json({
      data,
      total: total[0].count,
    });
  }
  // 获取已读通知
  static async getNoticeRead(req, res) {
    let verify = await jwtUtil.verifysync(req.query.token, global.globalKey);
    if (verify.err == "error") {
      return res.json(verify.msg);
    }
    let user_id = verify.id;
    // console.log(user_id);
    let data = await this.getNoticeReadMod(user_id);
    res.json(data);
  }

  // 已读转未读
  static async goUnRead(req, res) {
    let verify = await jwtUtil.verifysync(req.query.token, global.globalKey);
    if (verify.err == "error") {
      return res.json(verify.msg);
    }

    let user_id = verify.id;
    let notice_id = req.query.n_id;

    let data = await this.goUnReadMod(user_id, notice_id);
    res.json(data);
  }

  // 未读转已读
  static async goReaded(req, res) {
    let verify = await jwtUtil.verifysync(req.query.token, global.globalKey);
    if (verify.err == "error") {
      return res.json(verify.msg);
    }

    let user_id = verify.id;
    let notice_id = req.query.n_id;

    let data = await this.goReadedMod(user_id, notice_id);
    res.json(data);
  }

  /* -----------------填表部分------------ */

  // 提交健康申报表
  static async setHeath(req, res) {
    let { token, temperature, godanger, fever, leaveout, hesuan, mask, kills } =
      req.body;
    let verify = await jwtUtil.verifysync(token, global.globalKey);
    if (verify.err == "error") {
      return res.json(verify.msg);
    }
    let u_id = verify.id;

    let data = await this.setHeathMod(
      u_id,
      temperature,
      godanger,
      fever,
      leaveout,
      hesuan,
      mask,
      kills
    );
    res.json(data);
  }

  // 分页获取当天填报表的与总数量
  static async getHealthNowDayPage(req, res) {
    const now = new Date();
    const today = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(
      2,
      "0"
    )}-${String(now.getDate()).padStart(2, "0")}`;
    const startTime = today + " 00:00:00";
    const endTime = today + " 23:59:59";

    let { currPage, pageSize } = req.query;
    let data = await this.getHealthNowDayPageMod(
      startTime,
      endTime,
      currPage,
      pageSize
    );
    let total = await this.getHealthNowDayPageTotal(startTime, endTime);

    res.json({
      data,
      total: total[0].count,
    });
  }

  // 获取当天某用户报表
  static async getHealthNowDayById(req, res) {
    let verify = await jwtUtil.verifysync(req.query.token, global.globalKey);
    if (verify.err == "error") {
      return res.json(verify.msg);
    }
    let u_id = verify.id;
    const now = new Date();
    const today = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(
      2,
      "0"
    )}-${String(now.getDate()).padStart(2, "0")}`;
    const startTime = today + " 00:00:00";
    const endTime = today + " 23:59:59";
    let data = await this.getHealthNowDayByIdMod(u_id, startTime, endTime);

    res.json(data);
  }

  // 获取当天所有报表
  static async getHealthNowDay(req, res) {
    const date = new Date();
    const today =
      date.getFullYear() +
      "-" +
      String(date.getMonth() + 1).padStart(2, "0") +
      "-" +
      String(date.getDate()).padStart(2, "0");
    const startTime = `${today} 00:00:00`;
    const endTime = `${today} 23:59:59`;

    let data = await this.getHealthNowDayMod(startTime, endTime);

    res.json(data);
  }

  // 获取当月所有报表
  static async getHealthNowMonth(req, res) {
    let { nowMonth, lastMonth } = this.getNowAndLastDate();

    let data = await this.getHealthNowMonthMod(nowMonth, lastMonth);

    res.json(data);
  }

  // 获取所有报表
  static async getAllHealth(req, res) {
    let data = await this.getAllHealthMod();
    res.json(data);
  }

  // 学生请假表申请
  static async setLeave(req, res) {
    let verify = await jwtUtil.verifysync(req.body.token, global.globalKey);
    if (verify.err == "error") {
      return res.json(verify.msg);
    }
    let u_id = verify.id;
    let classes = verify.classes;
    let { reason, leavetype, start_time, end_time } = req.body;

    let results = await this.setLeaveMod(
      u_id,
      classes,
      reason,
      leavetype,
      start_time,
      end_time
    );
    res.json(results);
  }
};
